//
//  DraggableView.m
//  DraggingImage
//
//  Created by apple on 8/19/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DraggableView.h"
#import "PuzzleViewController.h"
#import "FixFrame.h"
#import "MovingCommand.h"
#import "GameAudio.h"

@implementation DraggableView{
    
    GameAudio *gameAudio;
}
@synthesize topView;
@synthesize bottomView;
@synthesize leftView;
@synthesize rightView;
@synthesize horizontalIndex;
@synthesize verticalIndex;
@synthesize targetVerticalIndex;
@synthesize targetHorizontalIndex;
@synthesize controller;  
@synthesize isRightPosition;

- (void)setBorder:(CGRect)frame withColor:(UIColor *)color {
    UIView *top = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)];
    UIView *left = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, frame.size.height)];
    UIView *right = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width, 0, 1, frame.size.height)];
    UIView *bottom = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height, frame.size.width, 1)];
    [top setBackgroundColor:color];
    [left setBackgroundColor:color];
    [right setBackgroundColor:color];
    [bottom setBackgroundColor:color];
    [self addSubview:top];
    [self addSubview:left];
    [self addSubview:right];
    [self addSubview:bottom];

}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        isRightPosition = YES;
        [self setBorder:frame withColor:[UIColor greenColor]];
        gameAudio = [[GameAudio alloc] init];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint startPoint = [[touches anyObject]locationInView:self];
    start = startPoint;
    //[[self superview] bringSubviewToFront:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGRect frame = self.frame;
    frame.origin.x += pt.x - start.x;
    frame.origin.y += pt.y - start.y;
    //[self setFrame:frame];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint pt = [[touches anyObject] locationInView:self];
    CGRect frame = self.frame;
    float xMove = pt.x - start.x;
    float yMove = pt.y - start.y;
    FixFrame *target = nil;
    MovingDirection fixFrameMovingDirection;
    //NSLog(@"Dragged (h,v) = (%d,%d)",self.horizontalIndex,self.verticalIndex);
    if (abs(xMove) > abs(yMove)) {
        //horizontal move
        frame.origin.x += xMove > 0 ?frame.size.width:-frame.size.width;
        if (xMove < 0) {
            fixFrameMovingDirection = MovingDirectionRight;
            target = [controller findFixFrameHorizontal:(self.horizontalIndex - 1) andVertical:self.verticalIndex];
            
        }else{
            fixFrameMovingDirection = MovingDirectionLeft;
            target = [controller findFixFrameHorizontal:(self.horizontalIndex + 1) andVertical:self.verticalIndex];
            
        }
    }else{
        if (yMove < 0) {
            fixFrameMovingDirection = MovingDirectionBottom;
            target = [controller findFixFrameHorizontal:self.horizontalIndex andVertical:(self.verticalIndex - 1)];
           
        }else{
            fixFrameMovingDirection = MovingDirectionTop;
            target = [controller findFixFrameHorizontal:self.horizontalIndex andVertical:(self.verticalIndex + 1)];
            
        }
    }
    
    if (target) {
        
        [self moveToTarget:target];
        [controller.movingCommand addDirection:fixFrameMovingDirection];
        
    }
    
}

- (void)moveToTarget:(FixFrame *)target {
    FixFrame *clearFixFrame = [[FixFrame alloc] initWithHorizontal:self.horizontalIndex andVertical:self.verticalIndex];
    [clearFixFrame setFixFrameValue:self.frame];
    [clearFixFrame setDraggableView:nil];
    [controller setClearFixFrame:clearFixFrame];
    
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.2];
    [self setVerticalIndex:target.verticalIndex];
    [self setHorizontalIndex:target.horizontalIndex];
    [self setFrame:target.fixFrameValue];
    [target setDraggableView:self];
    
    
    CGRect frame = self.frame;
    if (target.verticalIndex == self.targetVerticalIndex
        && target.horizontalIndex == self.targetHorizontalIndex) {
        [self setBorder:frame withColor:[UIColor greenColor]];  
        isRightPosition = YES;
    } else {
        [self setBorder:frame withColor:[UIColor whiteColor]];
        isRightPosition = NO;
    }
    
    [UIView commitAnimations];
    [gameAudio playDragAudio];
    [controller draggableViewDidMove];
}


@end
